//
//  main.c
//  C_Study
//
//  Created by rui on 12-6-30.
//  Copyright (c) 2012年 Wang Rui. All rights reserved.
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//king of moneky
void monkey()
{
#define monkeyCount 1000
	
	int a[monkeyCount] = {0}; //0为没喊到状态，1为喊道状态，喊道的猴子出局
	int countIndex = 0; //保障每三个没喊到的猴子出局一个
	int numbersEqualZeroCount = monkeyCount;//剩下的没喊到猴子数
	int indexOfKing = 0;
	
	while (numbersEqualZeroCount>1) {
		for (int i = 0; i<monkeyCount; i++) 
		{
			if (a[i] == 1) 
			{
				continue;
			}
			
			countIndex ++;
			if (countIndex == 3) {
				a[i] = 1;
				numbersEqualZeroCount--;
				countIndex = 0;
			}
		}
	}
	
	
	for (int i=0; i<monkeyCount; i++) {
		if (a[i]==0) {
			indexOfKing = i+1;
		}
	}
	
	printf("the numeber %d monkey is the king",indexOfKing);
}

int main(int argc, const char * argv[])
{	
	monkey();
	return 0;
}

